package view.Core;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class loginActivity extends Activity {

	private static final int REQUEST_CODE = 3434;// /login out

	boolean loginStatic = false;
	String loginName, Password;
	
	EditText loginEditText1, loginEditText2;
	Button loginButton1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		findViewById();
	}

	public void onClickLogin(View view){
		//http://localhost/oldTreeWeb/login.php?loginName=adam&loginPwd=adam&Login
		String url = getResources().getString(R.string.serverURL)+"login.php?"
				+ "loginName=" + loginEditText1.getText().toString()
				+ "&loginPwd=" + loginEditText2.getText().toString();
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			HttpResponse response = client.execute(request);
			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply = line;
			}
			reader.close();
			
			//outputString = reply;

				if (reply.equals("Login Successful.")){
					//loginState = true;
					toastMsg("login successful");
					Intent result = new Intent();
					result.putExtra("loginName", loginEditText1.getText().toString());
					result.putExtra("Password", loginEditText2.getText().toString());
					setResult(RESULT_OK, result);
					
					finish();
				}
				else
					toastMsg("login fail");
					//loginState = false;

		} catch (Exception e) {
			toastMsg(e+"");
			//loginState = false;
		}
	}
	
	public void toastMsg(String msg){
		
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
		
	}
	private void findViewById() {
		loginEditText1 = (EditText)findViewById(R.id.loginEditText1);
		loginEditText2 = (EditText)findViewById(R.id.loginEditText2);
		
		loginButton1 = (Button)findViewById(R.id.loginButton1);

	}
}